﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using RbmlVisualizer.Logging;

namespace RbmlVisualizer
{
    public class LoggerViewModel : ViewModelBase
    { 
        private readonly ObservableCollection<LogMessage> _logMessages = new ObservableCollection<LogMessage>();
       
        
        public LoggerViewModel()
        {
            SelectedLogComboItem = LogLevelType.Information;
            LoggingService.Instance.MessageLogged += new EventHandler<MessageLoggedEventArgs>(Instance_MessageLogged);
        }

        private void Instance_MessageLogged(object sender, MessageLoggedEventArgs e)
        {
            this.PopulateLoggingMessages();
        }

        private ObservableCollection<LogMessage> PopulateLoggingMessages()
        {
            _logMessages.Clear(); 
            foreach (var logMessage in LoggingService.Instance.GetLoggingMessagesForLevel(SelectedLogComboItem))
            {
                _logMessages.Add(logMessage);
            }
            return _logMessages; 
        }
 
        public ObservableCollection<LogMessage> LogMessages
        {
            get 
            {
                return PopulateLoggingMessages(); 
            }
        }

        private LogLevelType _selectedLogComboItem;
        public LogLevelType SelectedLogComboItem
        {
            get { return _selectedLogComboItem; }
            set
            {
                if (_selectedLogComboItem == value)
                    return;

                _selectedLogComboItem = (LogLevelType)value;
                PopulateLoggingMessages(); 
            }
        }
    }
}
